package pong.app.builders 
{
	import common.IApp;
	import common.IAppBuilder;
	import common.IStateController;
	import common.ObjectUtil;
	import common.StateController;
	
	import flash.display.DisplayObjectContainer;
	
	import pong.app.IPongAppController;
	import pong.app.IPongAppSkin;
	import pong.app.IPongAppSkinFactory;
	import pong.app.IPongAppState;
	import pong.app.IPongAppStateContext;
	import pong.app.IPongAppStateController;
	import pong.app.IPongAppView;
	import pong.app.controllers.PongAppController;
	import pong.app.controllers.PongAppDummyState;
	import pong.app.controllers.PongAppMenuState;
	import pong.app.controllers.PongAppStateContext;
	import pong.app.controllers.PongAppStateController;
	import pong.app.facade.PongAppFacade;
	import pong.app.factories.PongAppSkinFactory;
	import pong.app.views.PongAppMenuView;

	/**
	 * ...
	 * @author Luke Van In
	 */
	public class PongAppBuilder implements IAppBuilder
	{
	
		private var _skinFactory:IPongAppSkinFactory;
		
		private var _container:DisplayObjectContainer;
		
		
		
		public function PongAppBuilder(skinFactory:IPongAppSkinFactory, container:DisplayObjectContainer) 
		{
			this._skinFactory = skinFactory;
			
			this._container = container;
		}
		
		

		/**
		 * Builds an instance of an application
		 * 
		 * @return
		 */
		public function buildApp():IApp 
		{
			
			// create views
			var menuSkin:IPongAppSkin = this._skinFactory.createSkin(PongAppSkinFactory.MENU_STATE);
			
			var menuView:IPongAppView = new PongAppMenuView(menuSkin, this._container);
			
			
			
			// create state controller
			var stateController:IStateController = new StateController();
			
			// create state context, and assign states to it
			var stateContext:IPongAppStateContext = new PongAppStateContext(stateController);
			
			// create loading state
			var loadingState:IPongAppState = new PongAppDummyState(stateContext, "loading");
			
			stateContext.setLoadingState(loadingState);
			
			// create menu state
//			var menuState:IPongAppState = new PongAppDummyState(stateContext, "menu");
			
			var menuState:IPongAppState = new PongAppMenuState(stateContext, menuView);
			
			stateContext.setMenuState(menuState);
			
			
			// create login state
			var loginState:IPongAppState = new PongAppDummyState(stateContext, "login");
			
			stateContext.setLoginState(loginState);
			
			// create game state
			var playingState:IPongAppState = new PongAppDummyState(stateContext, "playing");
			
			stateContext.setPlayingState(playingState);
			
			
			// create scores state
			var scoresState:IPongAppState = new PongAppDummyState(stateContext, "scores");	
			
			stateContext.setScoresState(scoresState);
			
			
			// set a default state
			stateContext.gotoLoadingState();
			
			
			// create app state controller
			var appStateController:IPongAppStateController = new PongAppStateController(stateContext);
			
			
			// create app controller
			var appController:IPongAppController = new PongAppController(appStateController);
			
			
			// create app facade
			var app:IApp = new PongAppFacade(appController);
			
			return app;
		}
		
		
		
	}

}